<template>
	<!-- 我的素材 -->
	<view class="mymaterial">
		<!-- tabs -->
	<!-- 	<view class="tabs" v-if="list1 && list1.length>=2">
			<u-tabs :scrollable="true" :list="list1" class="tabs-list"
				:inactiveStyle="{'fontSize':'34rpx'}" :activeStyle="{'fontSize':'34rpx'}" lineBgSize="cover" itemStyle="height: 34px;" :current="currentSel.index" @click="clickTabs">
			</u-tabs>
		</view> -->
		<!-- 内容 -->
		<view style="padding: 30rpx;">
			<view class="itemlist" v-for="(item,index) in materialList">
				<view>
					<!-- 文本 -->
					<view class="itemText"  v-if="item.content&&item.content.length>0">
						{{item.content}}
					</view>
					<view class="itemImage" v-if="item.imagesPath&&item.imagesPath.length>0">
						<u-image @click="previewImage(item.imageurl)" :src="item.imageurl" width="110px" height="110px" mode="aspectFill"></u-image>
					</view>
				</view>
				<view class="itemRight" style="margin-right: 30rpx;">
					<!-- <view v-if="currentSel.index==2" @click="edieClick(item)" class="rightBtn rightUpdate">编辑</view> -->
					<view @click="deleteClick(item,index)" class="rightBtn rightDelete">删除</view>
				</view>
			</view>
		</view>
		<my-empty v-if="!materialList" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png">
		</my-empty>
		<u-loadmore v-else :status="status" />
		<view @click="toUrl(`/pages/newsPart/index/addmaterial`)" class="releaseBtn flex">
			<u-icon name="plus" color="#2979ff" size="36"></u-icon>
		</view>
	</view>
</template>
<script>
	import {
		getToken
	} from '@/utils/token.js'
	import {
		duplicateRemoval
	} from '@/utils/common.js'
	import {deletePortalNewsSource,searchPortalNewsSource} from '@/common/api/news.js'
	export default {
		data() {
			return {
				status: 'nomore', // nomore:没有更多 loadmore:加载更多 loading:加载中
				list1: [{
					name: '已通过',
					value: 1
				}, {
					name: '待审核',
					value: 0
				}, {
					name: '已拒绝',
					value: 2
				}],
				currentSel: {},
				materialList: [],
				currPage:1
			}
		},
		onLoad(option) {
			this.init() //1：已通过
		},
		onReachBottom:function(){
			if(this.status == 'loadmore'){
				this.currPage++ 
				this.init()
			}
		},
		methods: {
			previewImage(url){
				uni.previewImage({
					current: 0,
					urls: [`${url}`]
				})
			},
			deleteClick(item,index){
				//console.log('item',item)
				let that=this
				uni.showModal({
					title: '提示',
					content: '确定要删除该素材吗？',
					success: function (res) {
						if (res.confirm) {
							//删除
							let param={
								id:item.id
							}
							deletePortalNewsSource(param,'加载中').then(res=>{
								//console.log('删除',res)
								res = res.data
								if(res.code==200 && res.data==true){
									that.materialList.splice(index,1)
									uni.showToast({
										title:'删除成功！',
										icon:'none',
										duration: 2000
									})
								}
							})
						} else if (res.cancel) {
							
						}
					}
				});
				
			},
			init() {
				let params = {}
				params['craftsmanId'] = getToken('useNum')
				if (getToken('newsCompany')) {
					params['companyId'] = getToken('newsCompany')
				}
				params['state'] = 1
				params['page.current'] = this.currPage
				searchPortalNewsSource(params).then(res => {
					//console.log('查询成功', res)
					res = res.data
					if (res.code == 200) {
						let data =  res.data.records
						data.forEach((item,index)=>{
							if(item.imagesPath&&item.imagesPath.length>0){
								item.imageurl='https://www.ydylmold.cn/file/getPlayResource/108/'+item.imagesPath
							}
						})
						this.materialList = duplicateRemoval(this.materialList, data || [] ,'id')
						this.currPage < res.data.pages ? this.status = 'loadmore' : this.status = 'nomore'
						//console.log(this.status,888,this.materialList)
					}
				})
			},
			clickTabs(data) {
				//console.log("data",data)
				this.currentSel = data
				//console.log("this.currentSel", this.currentSel)
				this.materialList=[]
				this.init(data.value)
			},
			edieClick(item){
				uni.navigateTo({
					url: `/pages/newsPart/index/addmaterial?item=${encodeURIComponent(JSON.stringify(item))}`
				})
			},
			toUrl(url) {
				uni.navigateTo({
					url: url
				})
			},
			
		}
	}
</script>
<style lang="scss">
	.mymaterial {
		min-height: calc(100vh - var(--window-top));

		.tabs {
			width: 100%;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
		
		}

		.releaseBtn {
			width: 100rpx;
			height: 100rpx;
			background-color: white;
			border-radius: 10rpx;
			box-shadow: 0 0 6rpx 6rpx #DEE1E6;
			position: fixed;
			right: 20rpx;
			bottom: 100rpx;
		}

		.flex {
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.itemlist {
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: white;
			box-shadow: 0 0 10rpx 2rpx #DEE1E6;
			padding: 20rpx;
			margin-bottom: 20rpx;
		}

		.itemText {
			width: 450rpx;
			font-size: 30rpx;
			color: grey;
			white-space: normal;
			word-break: break-all;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			/* 行数 */
			-webkit-line-clamp: 3;
			overflow: hidden;
		}
		.rightBtn{
			padding: 6rpx 20rpx;
			border-radius: 10rpx;
			margin-bottom: 10rpx;
			color: white;
			font-size: 30rpx;
			font-weight: 600;
		}
		.rightUpdate{
			background-color: orange;
		}
		.rightDelete{
			background-color: red;
		}
		
	}
</style>
